<?php

/**
 * Project: CrazyPoems
 * Author: Alexandru
 * Date: 29/10/2008
 * File: admin-new-category.php
 */

// Load default settings.
require('inc/config.php');
require('inc/functions.misc.php');
require('inc/functions.index.php');
require('inc/functions.admin.php');

// Only admin has access.
if ($cookie->admin != 1) { header("Location: index.php"); }

$smarty->assign('pageTitle', 'Edit category');
$smarty->assign('breadcrumbs', '<a href="index.php">Home</a> &raquo; <a href="admin.php">Admin</a> &raquo; Edit category');

// Define variables.
$error_msg = "";
$categoryId = isset($_REQUEST['id']) && strlen($_REQUEST['id']) > 0 ? $_REQUEST['id'] : '';
$name = isset($_POST['name']) && strlen($_POST['name']) > 0 ? $_POST['name'] : '';

// Validate data.
if (isset($_POST['form_sent']) && $_POST['form_sent'] == "yes") {
	if (strlen($name) == 0) {
		$error_msg .= "<p> Please enter name. </p>";
	} else {
		$q = mysql_query("SELECT id FROM ".TABLE_CATEGORIES." WHERE name = '".checkSQL($name)."' AND id <> $categoryId") or die(mysql_error());
		$r = mysql_fetch_assoc($q);
		if ($r) {
			$error_msg .= "<p> That category already exist. </p>";
		}
	}
	// Add category.
	if (strlen($error_msg) == 0) {
       Hop_editCategory($categoryId, $name);
	}
} else {
	$q = mysql_query("SELECT * FROM ".TABLE_CATEGORIES." WHERE id = $categoryId") or die(mysql_error());
	$r = mysql_fetch_assoc($q);
		if ($r) {
			$name = $r['name'];
		}
}

// Smarty.
$smarty->assign('form_sent', $_POST['form_sent']);
$smarty->assign('error_msg', $error_msg);
$smarty->assign('name', $name);
$smarty->assign('categoryId', $categoryId);

// Go!
$smarty->display('admin-edit-category.tpl');

// End page.
require('inc/end.php');
?>
